package com.citycloud.ccuap.tc.sync.service.impl;


import com.citycloud.ccuap.tc.sync.dto.DepartmentListDto;
import com.citycloud.ccuap.tc.sync.dto.DepartmentQuery;
import com.citycloud.ccuap.tc.sync.mapper.AspectDepartmentMapper;
import com.citycloud.ccuap.tc.sync.service.DepartmentService;
import com.citycloud.ccuap.tc.common.page.CommonPager;
import lombok.extern.log4j.Log4j2;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Log4j2
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    @Lazy
    AspectDepartmentMapper departmentMapper;


    @Override
    public CommonPager<DepartmentListDto> getDepartmentImportList(DepartmentQuery page) {

        CommonPager<DepartmentListDto> resultPage = new CommonPager<DepartmentListDto>(page);
        resultPage.setTotalCount(departmentMapper.getImportDeptIdsTotalCount(page.getProjectId()));
        List<DepartmentListDto> result = new ArrayList<>();
        resultPage.setDataList(result);
        List<String> list = departmentMapper.getImportDeptIds(page);
        Set<String> set = new HashSet<>();
        if (list.size() > 0) {
            List<String> shouldBeRemovedDeptids = null;
            //这步操作。。我都不知道怎么解释了，简单说就是为了确保第N页的数据中不应该包含前面页面的节点数据。不然前端会展示出重复的节点数据
            if (page.getOffset() > 0) {
                shouldBeRemovedDeptids = Arrays.asList(list.get(0).split("/"));
                if (shouldBeRemovedDeptids.size() > 0) {
                    shouldBeRemovedDeptids = shouldBeRemovedDeptids.subList(0, shouldBeRemovedDeptids.size() - 1);
                }
            }
            for (String str : list) {
                //脏数据
                if (str == null || str.isEmpty()) {
                    log.error("脏数据！！project {} 的 full_id_path 为空！！", page.getProjectId());
                } else {
                    set.addAll(Arrays.asList(str.split("/")));
                }

            }
            //移除操作
            if (shouldBeRemovedDeptids != null) {
                for (String str : shouldBeRemovedDeptids) {
                    set.remove(str);
                }
            }


            StringBuffer stringBuffer = new StringBuffer();
            int count = 0;
            for (String deptId : set) {
                count++;
                stringBuffer.append(deptId).append(",");
                if (count % 500 == 0) {
                    String values = stringBuffer.toString();
                    result.addAll(departmentMapper.getImportDepartmentList(values.substring(0, values.length() - 1)));
                    stringBuffer.delete(0, stringBuffer.length());
                }
            }
            //最后尾巴不要漏掉
            if (stringBuffer.length() > 0) {
                String values = stringBuffer.toString();
                List<DepartmentListDto> importDepartmentList = departmentMapper.getImportDepartmentList(values.substring(0, values.length() - 1));
                result.addAll(importDepartmentList);
            }
        }

        return resultPage;
    }



}
